package es.android.alertes.objects;

import es.android.alertes.R;
import greendroid.graphics.drawable.DrawableStateSet;
import greendroid.graphics.drawable.MapPinDrawable;

import java.util.List;
import java.util.Random;

import android.content.Context;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;


public class ListAdapter extends ArrayAdapter<AlertaEntity> {

	private List<AlertaEntity> datos;
	private Context ctx;
	
    private static Random sRandom = new Random();

    private static final int[] PRESSED_STATE = {
        android.R.attr.state_pressed
    };
	
	public ListAdapter(Context context, int textViewResourceId,
			List<AlertaEntity> objects) {
		super(context, textViewResourceId, objects);
		datos = objects;
		ctx = context;
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		
		ViewHolder holder = null;
		
		if (convertView == null || !(convertView.getTag() instanceof ViewHolder))
		{
			LayoutInflater mInflater = LayoutInflater.from(ctx);
			convertView = mInflater.inflate(R.layout.row, null);
			
			holder = new ViewHolder();
			holder.txtNom = (TextView) convertView.findViewById(R.id.txtRowNom);
			holder.nivell = (ImageView) convertView.findViewById(R.id.viewRow);
			
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		
		ColorStateList pinCsl = createRandomColorStateList(false, datos.get(position).getNivel());
        ColorStateList dotCsl = createRandomColorStateList(true, datos.get(position).getNivel());
        Drawable drw = new MapPinDrawable(ctx.getResources(), pinCsl, dotCsl);
		
		holder.txtNom.setText(datos.get(position).getNombre());
		holder.nivell.setBackgroundDrawable(drw);
		
		return convertView;
	}
	
	@Override
	public AlertaEntity getItem(int position) {
		return datos.get(position);
	}
	
	class ViewHolder
	{
		TextView txtNom;
		ImageView nivell;
	}
	
	private ColorStateList createRandomColorStateList(Boolean dot, int i) {

        int[][] states = new int[2][];
        int[] colors = new int[2];

        int red = ctx.getResources().getColor(R.color.Red);
        int tred = ctx.getResources().getColor(R.color.TRed);
        int green = ctx.getResources().getColor(R.color.Green);
        int tgreen = ctx.getResources().getColor(R.color.TGreen);
        int orange = ctx.getResources().getColor(R.color.Orange);
        int torange = ctx.getResources().getColor(R.color.TOrange);
        
        if (dot)
        {
	        if (i > 3)
	        {
	            states[0] = PRESSED_STATE;
	            colors[0] = addRGB(tred, -50);
	
	            states[1] = DrawableStateSet.EMPTY_STATE_SET;
	            colors[1] = tred;
	        }
	        else if (i <= 1)
	        {
	            states[0] = PRESSED_STATE;
	            colors[0] = addRGB(green, -50);
	
	            states[1] = DrawableStateSet.EMPTY_STATE_SET;
	            colors[1] = green;
	        }
	        else
	        {
	            states[0] = PRESSED_STATE;
	            colors[0] = addRGB(orange, -50);
	
	            states[1] = DrawableStateSet.EMPTY_STATE_SET;
	            colors[1] = orange;
	        }
        } else {
	        if (i > 3)
	        {
	            states[0] = PRESSED_STATE;
	            colors[0] = addRGB(red, -50);
	
	            states[1] = DrawableStateSet.EMPTY_STATE_SET;
	            colors[1] = red;
	        }
	        else if (i <= 1)
	        {
	            states[0] = PRESSED_STATE;
	            colors[0] = addRGB(tgreen, -50);
	
	            states[1] = DrawableStateSet.EMPTY_STATE_SET;
	            colors[1] = tgreen;
	        }
	        else
	        {
	            states[0] = PRESSED_STATE;
	            colors[0] = addRGB(torange, -50);
	
	            states[1] = DrawableStateSet.EMPTY_STATE_SET;
	            colors[1] = torange;
	        }
        }
        
        
        
        return new ColorStateList(states, colors);
    }
	
    private static int addRGB(int color, int amount) {
        int r = constrain(Color.red(color) + amount, 0, 255);
        int g = constrain(Color.green(color) + amount, 0, 255);
        int b = constrain(Color.blue(color) + amount, 0, 255);
        return Color.rgb(r, g, b);
    }

    public static int constrain(int amount, int low, int high) {
        return amount < low ? low : (amount > high ? high : amount);
    }
}